﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Tareas.ServiceReference;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace Tareas
{
    public sealed partial class SharingPage
    {
        public SharingPage()
        {
            InitializeComponent();
        }

        public void Activate(ShareTargetActivatedEventArgs args)
        {
            getProjects();
            //TODO: Break out useful parameters from the activation context
            //ItemTitle.Text = args.ShareOperation.Data.Properties.Title;
            //ItemDescription.Text = args.ShareOperation.Data.Properties.Description;
            ItemDetails.Text = args.ShareOperation.Data.Properties.Title + args.ShareOperation.Data.Properties.Description;
            // Use the share button to commit the sharing request
            ShareButton.Click += (backSender, backArgs) =>
            {

                // TODO: Perform application-specific work to accept the shared item

                // TODO: Dismiss the sharing UI
            };

            Window.Current.Content = this;
            Window.Current.Activate();
        }

        async void getProjects()
        {
            TaskServiceClient client = new TaskServiceClient();
            try
            {
                this.ProjectsCombo.ItemsSource = await client.getProjectsAsync();
                this.ProjectsCombo.DisplayMemberPath = "Name";
                client.CloseAsync();
            }
            catch (Exception)
            {
                //Show a Message dialog
            }
        }

        private async void ShareButton_Click(object sender, RoutedEventArgs e)
        {
            /*A new Task gets added*/
            TaskServiceClient client = new TaskServiceClient();
            Project project = (ProjectsCombo.SelectedItem as Project);
            await client.addTaskAsync(new Tareas.ServiceReference.Task() { RelatedProject = project, Title = ItemDetails.Text });
            client.CloseAsync();
            Window.Current.Close();
        }
    }
}
